package ALog;

public class ConsLog extends ALog {
	private Entry first;
	private ALog rest;

	public ConsLog(Entry first, ALog rest) {
		this.first = first;
		this.rest = rest;
	}

	public String toString() {
		return this.first + "\n " + this.rest;
	}

	@Override
	public double miles() {
		return this.first.getDistance() + this.rest.miles();
	}

	@Override
	public ALog getLog(int month, int year) {
		if (this.first.sameMonthInAYear(month, year))
			return new ConsLog(this.first, this.rest.getLog(month, year));
		else {
			return this.rest.getLog(month, year);
		}

	}

	public boolean equals(Object o) {
		if (o == null || !(o instanceof ConsLog))
			return false;
		else {
			ConsLog that = (ConsLog) o;
			return this.first.equals(that.first) && this.rest.equals(that.rest);
		}
	}

	@Override
	public double Max() {
		return Math.max(this.first.getDistance(), this.rest.Max());
	}

	@Override
	public double Min() {

		return Math.min(this.first.getDistance(), this.rest.Min());
	}

	@Override
	public double TKCCB(int month, int year) {
		if (this.first.sameMonthInAYear(month, year))
			return this.first.getDistance() + this.rest.TKCCB(month, year);
		else {
			return this.rest.TKCCB(month, year);
		}
	}

	@Override
	public ALog sortByDistance() {
		return this.rest.sortByDistance().insertDistanceOrder(this.first);
	}

	@Override
	protected ALog insertDistanceOrder(Entry e) {
		if (e.hasDistanceShortThan(this.first))
			return new ConsLog(e, this);
		else
			return new ConsLog(this.first, this.rest.insertDistanceOrder(e));
	}

}